
package DAO;

import core.BlogException;
import core.Uzivatel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *DAO trida k Uzivateli
 * @author vojta
 */
public class UzivatelDAO {

    Connection cn;

    /**
     * Konstruktor
     * @param cn 
     */
    public UzivatelDAO(Connection cn) {
        this.cn = cn;
    }

    /**
     * Prida noveho uzivatele
     * @param c
     * @throws BlogSQLException
     */
    public void addUzivatel(Uzivatel u) throws BlogException {
        try {
            //---------Zalozime uzivatele------------------
            PreparedStatement stmt = cn.prepareStatement("INSERT INTO Uzivatele(zobrazovaneJmeno, login, heslo) VALUES(?,?,MD5(?))");
            stmt.setString(1, u.getZobrazovaneJmeno());
            stmt.setString(2, u.getLogin());
            stmt.setString(3, u.getHeslo());            
            
            stmt.executeUpdate();
            
            System.out.println(stmt);

        } catch (SQLException ex) {
            ex.printStackTrace();
            throw new BlogException("Chyba pri vytvareni Uzivatele!");
        }
    }

    /**
     * Zjisti jestli uzivatel jiz existuje
     * @param c
     * @return
     * @throws BlogSQLException
     */
    public boolean exists(Uzivatel u) throws BlogException {
        try {
            PreparedStatement stmt = cn.prepareStatement("SELECT * FROM Uzivatele WHERE login = ?");
            stmt.setString(1, u.getLogin());

            ResultSet rs = stmt.executeQuery();
                        
            return rs.next();
        } catch (SQLException ex) {
            throw new BlogException("Chyba pri overovani existence Uzivatele");
        }
    }

    /**
     * Vrati seznam vsech uzivatelu
     * @return
     * @throws BlogSQLException
     */
    public List<Uzivatel> getUzivatele() throws BlogException {
        List<Uzivatel> list = new ArrayList<Uzivatel>();
        try {
            System.out.println("Connection: " + cn);
            PreparedStatement stmt = cn.prepareStatement("SELECT * FROM Uzivatele ORDER BY zobrazovaneJmeno ASC");
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                Uzivatel u = new Uzivatel(rs.getString("login"), rs.getString("zobrazovaneJmeno"));                
                list.add(u);
            }

            return list;

        } catch (SQLException ex) {
            ex.printStackTrace();
            throw new BlogException("Chyba pri vvypisu Uzivatelu!");
        }
    }

    /**
     * Ziska objekt uzivatele podle jeho loginu
     * @param login - login uzivatele
     * @return Uzivatel
     * @throws BlogException 
     */
    public Uzivatel getUzivatel(String login) throws BlogException{
        try {
            PreparedStatement stmt = cn.prepareStatement("SELECT * FROM Uzivatele WHERE login=?");
            ResultSet rs = stmt.executeQuery();
            
            return new Uzivatel(rs.getString("login"), rs.getString("zobrazovaneJmeno"));

        } catch (SQLException ex) {
            ex.printStackTrace();
            throw new BlogException("Chyba pri vvypisu Uzivatele!");
        }
    }
    
    /**
     * Zvaliduje login a heslo uzivatele, tj. overi jestli se muze pripojit
     * @param u - Uzivatel
     * @return Uzivatel - kdyz je to ok, null - kdyz to neni ok
     * @throws BlogException 
     */
    public Uzivatel validateUzivate(Uzivatel u) throws BlogException {
        try {
            PreparedStatement stmt = cn.prepareStatement("SELECT * FROM Uzivatele WHERE login=? AND heslo=MD5(?)");
            stmt.setString(1, u.getLogin());
            stmt.setString(2, u.getHeslo());
            ResultSet rs = stmt.executeQuery();
            
            if (rs.next()) {
                Uzivatel uz = new Uzivatel(rs.getString("login"), rs.getString("zobrazovaneJmeno"));
                return uz;
            } else
                return null;

        } catch (SQLException ex) {
            ex.printStackTrace();
            throw new BlogException("Chyba pri validaci uzivatele!");
        }
    }
        
    /**
     * Odstrani uzivatele z DB
     * @param u - Uzivatel
     * @throws BlogException 
     */
    public void removeUzivatel(Uzivatel u) throws BlogException {
        try {
            PreparedStatement stmt = cn.prepareStatement("DELETE FROM Uzivatele WHERE login=?");
            stmt.setString(1, u.getLogin());
            
            System.out.println(stmt);
            
            stmt.executeUpdate();
            
        } catch (SQLException ex) {
            ex.printStackTrace();
            throw new BlogException("Chyba pri odstranovani uzivatele!");
        }
    }
    
}
